﻿using System;
using System.Linq;

namespace AcademyPopcorn
{
    class AcademyPopcornMain
    {
        const int WorldRows = 23;
        const int WorldCols = 40;
        const int RacketLength = 6;

        static void Initialize(Engine engine)
        {
            int startRow = 3;
            int startCol = 2;
            int endCol = WorldCols - 2;

            CreateWalls(engine);

            for (int i = startCol; i < endCol; i++)
            {
                Block currBlock = new Block(new MatrixCoords(startRow, i));

                engine.AddObject(currBlock);
            }

            Ball theBall = new Ball(new MatrixCoords(WorldRows / 2, 0), new MatrixCoords(-1, 1));

            engine.AddObject(theBall);

            //Ball meteoriteBall = new MeteoriteBall(new MatrixCoords(WorldRows / 2, 0), new MatrixCoords(-1, 1));
            
            //engine.AddObject(meteoriteBall);

            Racket theRacket = new Racket(new MatrixCoords(WorldRows - 1, WorldCols / 2), RacketLength);

            engine.AddObject(theRacket);
        }

        static void CreateWalls(Engine engine)
        {
            // ceilling wall
            for (int i = 1; i < WorldCols - 1; i++)
            {
                IndestructibleBlock ceillingBlock = new IndestructibleBlock(new MatrixCoords(0, i), '─');

                engine.AddObject(ceillingBlock);
            }

            // left and right side walls
            for (int i = 1; i < WorldRows; i++)
            {
                IndestructibleBlock leftSideBlock = new IndestructibleBlock(new MatrixCoords(i, 0), '│');
                IndestructibleBlock rightSideBlock = new IndestructibleBlock(new MatrixCoords(i, WorldCols - 1), '│');

                engine.AddObject(leftSideBlock);
                engine.AddObject(rightSideBlock);
            }

            // corner blocks of the wall
            IndestructibleBlock leftTopCorner = new IndestructibleBlock(new MatrixCoords(0, 0), '┌');
            IndestructibleBlock rightTopCorner = new IndestructibleBlock(new MatrixCoords(0, WorldCols - 1), '┐');

            engine.AddObject(leftTopCorner);
            engine.AddObject(rightTopCorner);
        }

        static void Main(string[] args)
        {
            IRenderer renderer = new ConsoleRenderer(WorldRows, WorldCols);
            IUserInterface keyboard = new KeyboardInterface();

            Engine gameEngine = new Engine(renderer, keyboard, 300);

            keyboard.OnLeftPressed += (sender, eventInfo) =>
            {
                gameEngine.MovePlayerRacketLeft();
            };

            keyboard.OnRightPressed += (sender, eventInfo) =>
            {
                gameEngine.MovePlayerRacketRight();
            };

            Initialize(gameEngine);

            //

            gameEngine.Run();
        }
    }
}
